//
//  ApplyVC.m
//  代理端
//
//  Created by sliver on 15/12/12.
//  Copyright (c) 2015年 sliver. All rights reserved.
//

#import "BalanceVC.h"
#import "StockVC.h"
#import "ApplyVC.h"
#import "AboutOurVC.h"
#import "ShoppingMainVC.h"
#import "OrderMainVC.h"
#import "HeadVC.h"
#import "Unit.h"
#import "AgentVC.h"
#import "WXDataService.h"
#import "UIImageView+WebCache.h"
@interface ApplyVC ()<UITextFieldDelegate,UITextViewDelegate,UIAlertViewDelegate>
{
    UITextField  *moneyTextField; //金额
    UITextView   *reasonTextView;//原因
    UITextField   *categoryTextView;//原因
    UILabel *reasonLab;
    UIView *mainView; //主页view
    UIView *menuview; //菜单页
}
@end

@implementation ApplyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadmenuView];
    [self loadNav];
    [self loadmainView];
}

-(void)loadmainView{
    //输入返利类目TextField
    categoryTextView = [[UITextField alloc]initWithFrame:CGRectMake(40, 100,ScreenW - 80, 40)];
    categoryTextView.delegate = self;
    categoryTextView.backgroundColor = [UIColor whiteColor];
    categoryTextView.font = [UIFont systemFontOfSize:17*KScr];
    categoryTextView.placeholder = @" 返利类目，如：2016.4.6-2016.5.5";
    [categoryTextView setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    categoryTextView.textAlignment = NSTextAlignmentLeft;
    categoryTextView.clearButtonMode = UITextFieldViewModeWhileEditing;
    categoryTextView.keyboardType = UIKeyboardTypeNumberPad;
    categoryTextView.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    categoryTextView.returnKeyType = UIReturnKeyDefault;
    [mainView addSubview:categoryTextView];
    //输入手机号TextField
    moneyTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 150,ScreenW - 80, 40)];
    moneyTextField.delegate = self;
    moneyTextField.backgroundColor = [UIColor whiteColor];
    moneyTextField.font = [UIFont systemFontOfSize:17*KScr];
    moneyTextField.placeholder = @" 请输入返利金额";
    [moneyTextField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    moneyTextField.textAlignment = NSTextAlignmentLeft;
    moneyTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    moneyTextField.keyboardType = UIKeyboardTypeNumberPad;
    moneyTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    moneyTextField.returnKeyType = UIReturnKeyDefault;
    [mainView addSubview:moneyTextField];
    //输入原因TextField
    reasonTextView = [[UITextView alloc]initWithFrame:CGRectMake(40, 200, ScreenW-80, 100)];
    reasonTextView.delegate = self;
    reasonTextView.textColor = [UIColor grayColor];
    reasonTextView.font = [UIFont systemFontOfSize:17*KScr];
    reasonTextView.textAlignment = NSTextAlignmentLeft;
    reasonTextView.backgroundColor = [UIColor whiteColor];
    reasonTextView.returnKeyType = UIReturnKeyDone;
    reasonTextView.layer.masksToBounds = YES;
    reasonTextView.layer.cornerRadius = 5;
    [mainView addSubview:reasonTextView];
    //原因
    reasonLab = [[UILabel alloc]initWithFrame:CGRectMake(40, 206, ScreenW-80, 20)];
    reasonLab.text = @" 请输入返利原因:";
    reasonLab.backgroundColor = [UIColor clearColor];
    reasonLab.layer.masksToBounds = YES;
    reasonLab.layer.cornerRadius = 5;
    reasonLab.textColor = [UIColor grayColor];
    reasonLab.font = [UIFont systemFontOfSize:17*KScr];
    [mainView addSubview:reasonLab];
    //登陆按钮
    UIButton *landBtn = [[UIButton alloc]initWithFrame:CGRectMake(60, ScreenH - 100, ScreenW - 120, 50)];
    landBtn.backgroundColor = DRINK_RED_COLOR;
    landBtn.layer.masksToBounds = YES;
    landBtn.layer.cornerRadius = 5;
    [landBtn setTitle:@"提交申请" forState:UIControlStateNormal];
    landBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [landBtn addTarget:self action:@selector(clickLand:) forControlEvents:UIControlEventTouchUpInside];
    [mainView addSubview:landBtn];
}

#pragma mark - 提交
-(void)clickLand:(UIButton *)senfer{
    
    if (moneyTextField.text.length == 0 || reasonTextView.text.length == 0 || categoryTextView.text.length == 0) {
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请完善信息" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        return;
    }
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:USER_ACCOUNT forKey:@"agency_id_"];
    [params setObject:moneyTextField.text forKey:@"cash"];
    [params setObject:reasonTextView.text forKey:@"reason"];
    [params setObject:categoryTextView.text forKey:@"category_time"];
//    NSLog(@"%@",params);
    [MyMbd addHudToView:self.view];
    [WXDataService requestWithURL:@"mobile/agency/applyRebates.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        [MyMbd hidenHudFromView:self.view];
        
        reasonLab.hidden = NO;
        
        reasonTextView.text=@"";
        
        moneyTextField.text = @"";
        BOOL suc =[success boolValue];
        if (suc == 0 ) {
            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [deleAlertView show];
            return;
        }else if(suc == 1){
            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:@"申请返利成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [deleAlertView show];
            return;
        }
    }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
#pragma mark - nav
-(void)loadNav{
    mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
    mainView.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    [self.view addSubview:mainView];
    UIView *redview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 20)];
    redview.backgroundColor = DRINK_RED_COLOR;
    [mainView addSubview:redview];
    
    UILabel *redLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, ScreenW, 40)];
    redLab.backgroundColor = DRINK_RED_COLOR;
    redLab.text = @"申请返利";
    redLab.textColor = [UIColor whiteColor];
    redLab.textAlignment =  NSTextAlignmentCenter;
    redLab.font = [UIFont boldSystemFontOfSize:17];
    [mainView addSubview:redLab];
    
    if (self.fromto==fromtypetoHome) {
        UIButton *backBtn2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 20,44, 44)];
        backBtn2.adjustsImageWhenHighlighted = NO;
        [backBtn2 setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [backBtn2 addTarget:self action:@selector(clickBack2) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:backBtn2];
        
    }else
    {
        UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 20,44, 40)];
        backBtn.adjustsImageWhenHighlighted = NO;
        [backBtn setBackgroundImage:[UIImage imageNamed:@"菜单"] forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(clickBack:) forControlEvents:UIControlEventTouchUpInside];
        [mainView addSubview:backBtn];
        
    }
}
#pragma mark - 菜单页
-(void)loadmenuView{
    menuview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW *0.75, ScreenH)];
    menuview.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    for(int i=1;i<9;i++){
        NSArray *menuAry = @[@"    首页",@"    商城",@"    订单",@"    余额",@"    库存",@"    申请返利",@"    关于我们",@"    退出"];
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 100+41*i, ScreenW *0.75, 40)];
        btn.tag = i;
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitle:[menuAry objectAtIndex:i-1] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(clickmenu:) forControlEvents:UIControlEventTouchUpInside];
        [menuview addSubview:btn];
    }
    //上面头像等
    UIImageView*imageview = [[UIImageView alloc]init];
    imageview.backgroundColor = [UIColor whiteColor];
    NSString *string =  [NSString stringWithFormat:@"%@%@",BASE_URL,USER_IMG];
    NSURL *imgurl = [NSURL URLWithString:string];
    [imageview setImageWithURL:imgurl placeholderImage:[UIImage imageNamed:@"代理商信息头像_150.png"]];
    imageview.frame =CGRectMake(20, 40, 80, 80);
    imageview.layer.masksToBounds = YES;
    imageview.layer.cornerRadius = 5;
    imageview.userInteractionEnabled = YES;
    //单击手势
    UITapGestureRecognizer *sigleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(signalTapAction)];
    [imageview addGestureRecognizer:sigleTap];
    [menuview addSubview:imageview];
    
    //名称
    UILabel *nameLab = [[UILabel alloc]initWithFrame:CGRectMake(120, 40, ScreenW *0.75 - 120, 20)];
    nameLab.text = CPOMANY_NAME;
    nameLab.font = [UIFont systemFontOfSize:15];
    [menuview addSubview:nameLab];
    //等级
    UILabel *levelLab = [[UILabel alloc]initWithFrame:CGRectMake(120, 65, ScreenW *0.75 - 120, 20)];
    levelLab.font = [UIFont systemFontOfSize:15];
    switch ([USER_LEVEL integerValue]) {
        case 1:
            levelLab.text =@"省级代理";
            break;
        case 2:
            levelLab.text =@"市级代理";
            break;
        case 3:
            levelLab.text =@"县级代理";
            break;
        case 4:
            levelLab.text =@"采集代理";
            break;
            
        default:
            break;
    }
    [menuview addSubview:levelLab];
    
    //中间竖线
    UIView *vieww = [[UIView alloc]initWithFrame:CGRectMake(ScreenW *0.75-1, 0, 1, ScreenH)];
    vieww.backgroundColor = [UIColor blackColor];
    vieww.layer.shadowColor = [[UIColor blackColor]CGColor];
    vieww.layer.shadowRadius = 3;
    vieww.layer.shadowOpacity = 0.9;
    vieww.layer.shadowOffset = CGSizeMake(-3, 0);
    [menuview addSubview:vieww];
    [self.view addSubview:menuview];
}
-(void)clickBack2
{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 点击菜单
-(void)clickmenu:(UIButton *)sender{
    NSArray *array = @[@"HeadVC",@"ShoppingMainVC",@"OrderMainVC",@"BalanceVC",@"StockVC",@"ApplyVC",@"AboutOurVC",@"LandingVC"];
    if (sender.tag<8) {
        NSDictionary *dic = [NSDictionary dictionary];
        dic = @{@"vc":array[sender.tag-1]};
        
//        NSLog(@"%@",dic);
        
        if (sender.tag-1==0) {
            [self dismissViewControllerAnimated:NO completion:^{}];
            
        }else
        {
            if (sender.tag == 8) {
                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"tuichudenglu"];
            }
            [self dismissViewControllerAnimated:NO completion:^{}];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"ceshi" object:self userInfo:dic];
            
            
        }
        
    }else
    {
        AppDelegate *dele = (AppDelegate *)[[UIApplication sharedApplication]delegate] ;
        NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
        [userDefaultes setObject: nil forKey:@"USER_ID"];
        [userDefaultes setObject:nil forKey:@"USER_PWD"];
        [userDefaultes setObject:nil forKey:@"USER_IMG"];
        [userDefaultes setObject:nil forKey:@"USER_LEVEL"];
        [userDefaultes setObject:nil forKey:@"USER_ACCOUNT"];
        [userDefaultes setObject:nil forKey:@"USER_DIC"];
        [userDefaultes setObject:nil forKey:@"CPOMANY_NAME"];
        [userDefaultes synchronize];
        [dele showMain];
        
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    reasonLab.hidden = YES;
    
}
//-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
//    reasonLab.text = @"";
//    return YES;
//}
-(void)viewTapAction{
    [UIView animateWithDuration:0.2 animations:^(){
        mainView.frame = CGRectMake(0, 0, ScreenW, ScreenH);
    }];
}
#pragma mark - 菜单按钮
-(void)clickBack:(UIButton *)sender{
    sender.selected = !sender.selected;
    if(sender.selected){
        [UIView animateWithDuration:0.2 animations:^(){
            mainView.frame = CGRectMake(ScreenW*0.75, 0, ScreenW, ScreenH);
        }];
        
        UIView *shoushiView = [[UIView alloc]initWithFrame:CGRectMake(ScreenW*0.75, 0, ScreenW*0.25, ScreenH)];
        shoushiView.tag = 1999;
        shoushiView.userInteractionEnabled = YES;
        shoushiView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:shoushiView];
        //单击手势
        UITapGestureRecognizer *sigleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewTapAction)];
        //设置点击手指的根数
        sigleTap.numberOfTouchesRequired = 1;
        //点击屏幕次数
        sigleTap.numberOfTapsRequired = 1;
        [shoushiView addGestureRecognizer:sigleTap];
        
        
    }else{
        UIView *view = (UIView *)[self.view viewWithTag:1999];
        [view removeFromSuperview];
        [UIView animateWithDuration:0.2 animations:^(){
            mainView.frame = CGRectMake(0, 0, ScreenW, ScreenH);
        }];
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [moneyTextField resignFirstResponder];
    
    return YES;
    
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [moneyTextField resignFirstResponder];
    [reasonTextView resignFirstResponder];
    if (reasonTextView.text.length ==0) {
        reasonLab.hidden = NO;
    }else
    {
        reasonLab.hidden = YES;
    }
    
}
#pragma mark - 点击头像
-(void)signalTapAction{
    [self dismissViewControllerAnimated:NO completion:nil];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:@"AgentVC" forKey:@"vc"];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"ceshi" object:nil userInfo:dic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if ([alertView.message isEqualToString:@"申请返利成功"]) {
        
        HeadVC *headvc = [[HeadVC alloc]init];
        headvc.modalPresentationStyle = UIModalTransitionStyleCoverVertical;
        [self presentViewController:headvc animated:NO completion:nil];
        
    }
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
